<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------
namespace app\user_center\behavior\user\login_mobile_balance_before;

/**
 * 首次登录校验
 * @package app\user_center\behavior\user
 * @author shiqiang
 */
class checkFirstLogic{
    /**
     * 执行行为 run方法是Behavior唯一的接口
     * @access public
     * @return void
     */
    public function run($params){
		$mobile 		= $params['mobile'];
		$f_uid 			= $params['f_uid'];//推荐人uid
		$create_time	= time();
        $balance	    = $params['balance'];
		//查询是否注册
		$map = [];
		$map['mobile'] = $mobile;
		$result = model('user_center/UcMemberMobile', 'model\user') -> where($map) -> find();

		if(empty($result)){
			//增加一个机制 号码段14400000001 - 14410000000 之间免密登录
			if(14490000001<=$mobile && $mobile <= 14499999999){
				$nick_name = get_round_username();
				$sex = mt_rand(1,2);
				$birthday = date('Y-m-d', mt_rand(672543717,925008117));
			}else{
				$nick_name='MXP_'.mt_rand(100000,999999);
				$sex=0;
				$birthday = null;
				$create_time = '';
			}

			//注册插入默认数据
			$result = model('user_center/Member', 'model\user') -> addUser($nick_name, $sex, $autograph='', $avatar_url='', $birthday, $f_uid, $create_time, $balance);
			//dump($result);
			if($result['code'] > 0){
				//插入手机表
				$uid = $result['data'];
				$result = model('user_center/UcMemberMobile', 'model\user') -> addData($uid, $mobile, $create_time);

				if($result['code'] < 0){
					return array_out(-1001, '登陆失败-手机注册失败');
				}
				
				// 注册发放优惠券
				auto_receive_coupon($uid, 0);
			}else{
				return array_out(-1001, '登陆失败-注册失败');
			}
		}
    }
}
